<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="光纤通道" prop="fiberNum">
        <el-select v-model="queryParams.fiberNum" placeholder="请选择光纤通道" clearable>
          <el-option
              v-for="dict in fiber_info"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模型id" prop="modelId">
                <el-select v-model="queryParams.modelId" placeholder="请选择模型名称" clearable>
                  <el-option v-for="dict in model3Dict" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
<!--        <el-input
            v-model="queryParams.modelId"
            placeholder="请输入模型id"
            clearable
            @keyup.enter="handleQuery"
        />-->
      </el-form-item>
      <el-form-item label="分析结果" prop="analysisResult">
        <el-select v-model="queryParams.analysisResult" placeholder="请选择分析结果" clearable>
          <el-option
              v-for="dict in analysis_flag"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="预警结果" prop="warnResult">
        <el-input
            v-model="queryParams.warnResult"
            placeholder="请输入预警结果"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['ba:baModel3His:add']"
        >新增</el-button>
      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['ba:baModel3His:edit']"
        >修改</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Refresh"
            :disabled="multiple"
            @click="handleReAnalysis"
            v-hasPermi="['ba:baModel3His:edit']"
        >重新分析</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['ba:baModel3His:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['ba:baModel3His:export']"
        >导出</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="DataLine"
            @click="handleEChartLine"
            v-hasPermi="['ba:baModel3His:add']"
        >折线图</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="baModel3HisList" @selection-change="handleSelectionChange" stripe>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流水号" align="center" prop="id" v-if="false"/>
      <el-table-column label="光纤通道编号" align="center" prop="fiberNum">
        <template #default="scope">
          <dict-tag :options="fiber_info" :value="scope.row.fiberNum"/>
        </template>
      </el-table-column>
      <el-table-column label="模型id" align="center" prop="modelId" >
        <template #default="scope">
          <dict-tag :options="model3Dict" :value="scope.row.modelId"/>
        </template>
      </el-table-column>
      <el-table-column label="预警接口保存表id" align="center" prop="apiId" v-if="false"/>
      <el-table-column label="分析结果" align="center" prop="analysisResult" >
        <template #default="scope">
          <dict-tag :options="analysis_flag" :value="scope.row.analysisResult"/>
        </template>
      </el-table-column>
      <el-table-column label="分析文件路径" align="center" prop="analysisFilePath"  v-if="false"/>
      <el-table-column label="预警结果" align="center" prop="warnResult" >

        <template #default="scope">
          <dict-tag :options="warn_level" :value="scope.row.warnResult"/>
        </template>
      </el-table-column>
      <el-table-column label="最大挠度值" align="center" prop="yzDispMax" />
      <el-table-column label="预警文件路径" align="center" prop="warnFilePath" v-if="false"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Refresh" @click="handleReAnalysis(scope.row)" v-hasPermi="['ba:baModel3His:edit']">重新分析</el-button>
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ba:baModel3His:edit']">修改</el-button>-->
          <el-button link type="primary" icon="Memo" @click="handleShowDetail(scope.row)" v-hasPermi="['ba:baModel3His:remove']">分析结果</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ba:baModel3His:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改崩岸模型3历史记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="baModel3HisRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="光纤通道编号" prop="fiberNum">
          <el-select v-model="form.fiberNum" placeholder="请选择光纤通道编号">
            <el-option
                v-for="dict in fiber_info"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模型id" prop="modelId">
          <el-input v-model="form.modelId" placeholder="请输入模型id" />
        </el-form-item>
        <el-form-item label="预警接口保存表id" prop="apiId">
          <el-input v-model="form.apiId" placeholder="请输入预警接口保存表id" />
        </el-form-item>
        <el-form-item label="分析结果" prop="analysisResult">
          <el-input v-model="form.analysisResult" placeholder="请输入分析结果" />
        </el-form-item>
        <el-form-item label="分析文件路径" prop="analysisFilePath" v-if="false">
          <el-input v-model="form.analysisFilePath" placeholder="请输入分析文件路径" />
        </el-form-item>
        <el-form-item label="预警结果" prop="warnResult">
          <el-input v-model="form.warnResult" placeholder="请输入预警结果" />
        </el-form-item>
        <el-form-item label="预警文件路径" prop="warnFilePath" v-if="false">
          <el-input v-model="form.warnFilePath" placeholder="请输入预警文件路径" />
        </el-form-item>
        <el-form-item label="最大挠度值" prop="yzDispMax">
          <el-input v-model="form.yzDispMax" placeholder="请输入最大挠度值" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog :title="modelTitle" v-model="modelOpen" width="600px" :fullscreen="true"
               :close-on-press-escape="false" append-to-body>
      <model3-detail-viewer ref="modelRef" :model3-his="modelInfo"></model3-detail-viewer>
    </el-dialog>


    <el-dialog :title="chartLineTitle" v-model="chartLineOpen" width="600px" :fullscreen="true"
               :close-on-press-escape="false" append-to-body>
      <model3-his-line-viewer ref="chartLineRef" :model3-his="chartLineInfo"></model3-his-line-viewer>
    </el-dialog>
  </div>
</template>

<script setup name="BaModel3His">
import { listBaModel3His, getBaModel3His, delBaModel3His, addBaModel3His, updateBaModel3His, reAnalysis } from "@/api/ba/baModel3His";
import model3DetailViewer from '@/views/ba/baModel3His/model3DetailVo.vue'
import model3HisLineViewer from '@/views/ba/baModel3His/model3HisLine.vue'
import {model3DictList} from '@/utils/baCommon'

const { proxy } = getCurrentInstance();
const { fiber_info, analysis_flag, warn_level } = proxy.useDict('fiber_info', 'analysis_flag', 'warn_level');
const { model3Dict } =  model3DictList()
// console.log(model3Dict)

const baModel3HisList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const modelOpen = ref(false)
const modelTitle = ref("")
const modelInfo = ref({})
const modelRef = ref(null)

const chartLineOpen = ref(false)
const chartLineTitle = ref("")
const chartLineInfo = ref({})
const chartLineRef = ref(null)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    fiberNum: null,
    modelId: null,
    analysisResult: null,
    warnResult: null,
  },
  rules: {
    fiberNum: [
      { required: true, message: "光纤通道编号不能为空", trigger: "change" }
    ],
    modelId: [
      { required: true, message: "模型id不能为空", trigger: "blur" }
    ],
    apiId: [
      { required: true, message: "预警接口保存表id不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询崩岸模型3历史记录列表 */
function getList() {
  loading.value = true;
  listBaModel3His(queryParams.value).then(response => {
    baModel3HisList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    fiberNum: null,
    modelId: null,
    apiId: null,
    analysisResult: null,
    analysisFilePath: null,
    warnResult: null,
    warnFilePath: null,
    yzDispMax: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("baModel3HisRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加崩岸模型3历史记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getBaModel3His(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改崩岸模型3历史记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["baModel3HisRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBaModel3His(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBaModel3His(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除崩岸模型3历史记录编号为"' + _ids + '"的数据项？').then(function() {
    return delBaModel3His(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('ba/baModel3His/export', {
    ...queryParams.value
  }, `baModel3His_${new Date().getTime()}.xlsx`)
}

function handleReAnalysis(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认重新分析崩岸模型3历史记录编号为"' + _ids + '"的数据项？').then(function() {
    loading.value = true;
    return reAnalysis(_ids);
  }).then(() => {
    loading.value = false;
    getList();
    proxy.$modal.msgSuccess("重新分析成功");
  }).catch(() => {
    loading.value = true;
  });
}

function handleShowDetail(row) {

  let modelId = row.modelId

  let modelName = null
  for (let item of model3Dict.value) {
    // console.log(item)
    if (item.value === modelId) {
      modelName = item.label
      break;
    }
  }
  // console.log(`modelName:${modelName}`)
  // let res = model3Dict.value.some((v) => v.value == modelId)
  // console.log(`res--${res}`)

  modelTitle.value = `崩岸模型3-${modelName}[${row.createTime}]-分析明细`
  modelInfo.value = row
  modelOpen.value = true
  let newPromise = new Promise((resolve) => {
    resolve()
  })
  newPromise.then(() => {
    modelRef.value.init()
  })
}

function handleEChartLine(){
  chartLineTitle.value = `崩岸模型3-折线图`
  chartLineInfo.value = {}
  chartLineOpen.value = true
  let newPromise = new Promise((resolve) => {
    resolve()
  })
  newPromise.then(() => {
    chartLineRef.value.init()
  })
}

getList();
</script>
